﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LithuanianOutLoud
{
    public partial class Form1 : Form
    {
        public RSSFeedParser feed = new RSSFeedParser();

        public Form1()
        {   
            InitializeComponent();
            button4.Enabled = false;
            button1.Enabled = false;
            webBrowser1.DocumentText = "<b>Loading podcasts...</b>";
            backgroundWorker1.RunWorkerAsync();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo("http://lithuanian.libsyn.com/");
            Process.Start(sInfo);
        }

        // Reload rss feed
        private void button4_Click(object sender, EventArgs e)
        {
            button4.Enabled = false;
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            webBrowser1.DocumentText = "<b>Re-loading podcasts...</b>";
            backgroundWorker1.RunWorkerAsync();
        }

        private void loadFeed()
        {
            feed.ParseRssFeed("http://lithuanian.libsyn.com/rss");

            if (feed.LOOMediaItems.Count > 0)
            {
                var myPlaylist = axWindowsMediaPlayer1.playlistCollection.newPlaylist("LithuanianOutLoud");
                
                foreach (LOOMediaItem item in feed.LOOMediaItems)
                { 
                    myPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(item.mediaUrl));
                }
                axWindowsMediaPlayer1.currentPlaylist = myPlaylist;

                if (feed.LOOMediaItems.Count > 0)
                {
                    webBrowser1.DocumentText = feed.LOOMediaItems[0].description;
                    label1.Text = feed.LOOMediaItems[0].title;
                }
            }
        }
        

        

        // Change has been made, update description
        private void axWindowsMediaPlayer1_CurrentItemChange(object sender, AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent e)
        {
            var text = axWindowsMediaPlayer1.currentMedia.sourceURL;
            foreach (LOOMediaItem item in feed.LOOMediaItems)
            {
                if (item.mediaUrl.Equals(text))
                {
                    webBrowser1.DocumentText = item.description;
                    label1.Text = item.title;
                    break;
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            loadFeed();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            button4.Enabled = true;
            button1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogBox dlg = new DialogBox(this);
            dlg.ShowDialog();

            if (dlg.selectedValue >= 1 && dlg.selectedValue <= feed.LOOMediaItems.Count)
            {
                // Skip forward in playlist
                axWindowsMediaPlayer1.Ctlcontrols.stop();
                axWindowsMediaPlayer1.Ctlcontrols.currentItem = axWindowsMediaPlayer1.currentPlaylist.get_Item(dlg.selectedValue-1);
                
            }
            
        }
    }
}
